home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7575 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: Tue, 27 Feb 1996 14:54:24 GMT
  6. Organization: Netcom
  7. Message-ID: <31331a38.54160408@nntp.ix.netcom.com>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com>
  9. NNTP-Posting-Host: ix-dc12-08.ix.netcom.com
  10. X-NETCOM-Date: Tue Feb 27  6:54:14 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. gg@isac.hces.com (Greg Goodrich) wrote:
  14.  
  15. > Abu Wawda (wawda@alcor.usc.edu) wrote:
  16. > : I'm having trouble understanding what the address of a static array
  17. > : is. For example, if I declare a variable called myarray as:
  18. > :     char myarray[10];
  19. > : then what could &myarray possibly mean? myarray is not a pointer, so
  20. > : &myarray could not possibly be the address of the variable myarray
  21. > : (like it would be if I did char* myarray and then asked for &myarray).
  22. > : Functions such as scanf() allow the following:
  23. > :     char myarray[10];
  24. > :     scanf("%s",&myarray);
  25. > : but I don't understand what scanf() could possibly be taking in the
  26. > : second parameter. It can't be: char** since myarray is not a
  27. > : pointer. I CAN understand how the following would work:
  28. > This is because C treats the occurrence of array names as the address of
  29. > the array.  Therefore the following are equivalent:
  30. >     scanf("%s", myarray);
  31. >     scanf("%s", &myarray);
  32. > The thing is, the address is implied when you use an array name.  This
  33. > is not so for pointers.  I am not sure why C was incorporated in this
  34. > way.  In my humble opinion, it would make it easier and clearer to force
  35. > the programmer to put the & in front of array names, the same as you
  36. > have to do for any other storage class.
  37.  
  38. No.  The are not equivalent.  The first is legal and the second is
  39. not.
  40.  
  41. The %s format item in scanf expects a pointer to char.  myarray is
  42. converted to a pointer to char so it is legal.  &myarray is a pointer
  43. to array of 10 char and is not converted.  This results in undefined
  44. behavior.
  45.  
  46. In many implementations pointer to char and pointer to array of 10
  47. char have the same representation and this will work properly, but
  48. this is not required by the standard.
  49.  
  50.  
  51. Michael M Rubenstein
  52.